define(['jquery', 'bootstrap', 'userend', 'table', 'form'], function ($, undefined, Userend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/course' + location.search,
                    add_url: 'user/course/add',
                    edit_url: 'user/course/edit',
                    multi_url: 'user/multi',
                    import_url: 'user/import',
                    table: 'course'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('题库编号')},
                        {field: 'name', title: __('题库名称')},
                        {field: 'code', title: __('题库编码')},
                        {
                            field: 'desc',
                            title: __('题库描述'),
                            searchable:false,
                        },
                        {
                            field: 'remark',
                            title: __('题库备注'),
                            searchable:false,
                        },
                        {
                            field: 'a_time',
                            title: __('添加时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.date
                        },
                        {
                            field: 'u_time',
                            title: __('更新时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.date
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'add',
                                    text: __('添加分类'),
                                    classname: 'btn btn-primary btn-xs btn-addtabs',
                                    icon: 'fa btn-edit fa-plus',
                                    url: 'user/questioncategory/index/ids/{ids}'
                                },
                                // {
                                //     name: 'add',
                                //     text: __('扫码学习'),
                                //     classname: 'btn btn-primary btn-xs btn-dialog',
                                //     icon: 'fa fa-qrcode',
                                //     url: 'user/course/qrcode/ids/{ids}'
                                // }
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 单个添加题目点击事件
            $('.btn-add-question1').click(function () {
                var ids = Table.api.selectedids(table);
                if (ids.length > 10) {
                    return;
                }
                var url = 'user/question/add1/ids/' + ids;
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('单个添加题目'), $(this).data() || {}, {
                    success: function (data, ret) {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 添加分类点击事件
            $('.btn-edit-chapter').click(function () {
                var ids = Table.api.selectedids(table);
                if (ids.length > 10) {
                    return;
                }
                var url = 'user/questioncategory/index/ids/' + ids;
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('添加分类'), $(this).data() || {}, {
                    success: function (data, ret) {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });


            // 监听导入题库事件
            $('.btn-add-question').click(function (){
                var selectIds = Table.api.selectedids(table);
                var url = 'user/course/importquestion/ids/'+selectIds.join(',');
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('导入题目'), $(this).data() || {}, {
                    success: function () {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 监听导出题库事件
            $('.btn-export-question').click(function (){
                var selectIds = Table.api.selectedids(table);
                var url = 'course/exportQuestion/ids/'+selectIds.join(',');
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('导出题目'), $(this).data() || {}, {
                    success: function () {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        qrcode: function () {
            Controller.api.bindevent();
        },
        importquestion:function (){
            $("#c-paper_id").data("params", function (obj) {
                return {custom: {course_id: $("#c-course_id").val()}};
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});